//
//  ZyaFoodOrdAlert.swift
//  SlaughterClient
//
//  Created by hudan on 2025/8/7.
//  Copyright © 2025 zwb. All rights reserved.
//

import UIKit

class ZyaFoodOrdAlert: UIView {
    
    private lazy var contentView: UIView = {
        let view = UIView().extBgColor(color: .white)
            .extRadius(radius: 10).extMaskBounds(mask: true)
        
        return view
    }()
    
    private var sureBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func initUI() {
        extBgColor(color: .init(white: 0, alpha: 0.5))
        
        addSubview(contentView)
        contentView.frame = .init(x: 20, y: 0, width: KS_SCREEN_WIDTH - 2*20, height: 10)
        
        let sureBtn = UIButton().easy("确定", .systemRed, .systemFont(ofSize: 17), nil, self, #selector(sureBtnClick))
        contentView.addSubview(sureBtn)
        sureBtn.snp.makeConstraints { make in
            make.left.bottom.equalToSuperview()
            make.right.equalTo(contentView.snp.centerX)
            make.height.equalTo(40)
        }
        
        let cancelBtn = UIButton().easy("取消", .systemBlue, .systemFont(ofSize: 17), nil, self, #selector(hide))
        contentView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { make in
            make.right.bottom.equalToSuperview()
            make.left.equalTo(contentView.snp.centerX)
            make.height.equalTo(40)
        }
        
        let line = UIView().extBgColor(color: UIColor(kshex: "#dddddd"))
        contentView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(0.8)
            make.bottom.equalTo(sureBtn.snp.top)
        }
        
        let line2 = UIView().extBgColor(color: UIColor(kshex: "#dddddd"))
        contentView.addSubview(line2)
        line2.snp.makeConstraints { make in
            make.width.equalTo(0.8)
            make.centerX.bottom.equalToSuperview()
            make.top.equalTo(line.snp.bottom)
        }
    }
    
    func show(infos: [(String, String)], sureBlock: @escaping (() -> Void)) {
        
        if let window = getKeyWindow() {
            
            self.sureBlock = sureBlock

            for (idx, info) in infos.enumerated() {
                let leftLab = UILabel().easy(info.0, .black, .systemFont(ofSize: 17))
                contentView.addSubview(leftLab)
                leftLab.snp.makeConstraints { make in
                    make.left.equalToSuperview().offset(15)
                    make.top.equalToSuperview().offset(10 + idx*30)
                    make.right.equalTo(contentView.snp.centerX)
                    make.height.equalTo(30)
                }
                
                let rightLab = UILabel().easy(info.1, .black, .systemFont(ofSize: 17))
                    .extTextAligment(aligment: .right)
                contentView.addSubview(rightLab)
                rightLab.snp.makeConstraints { make in
                    make.right.equalToSuperview().offset(-15)
                    make.top.height.equalTo(leftLab)
                    make.left.equalTo(contentView.snp.centerX)
                }
            }
            
            contentView.ks_height = CGFloat(60 + infos.count*30)
            
            window.addSubview(self)
            
            extBgColor(color: .init(white: 0, alpha: 0.01))
            contentView.ks_centerY = ks_height + contentView.ks_height
            UIView.animate(withDuration: 0.2) {
                self.extBgColor(color: .init(white: 0, alpha: 0.5))
                self.contentView.ks_centerY = self.ks_centerY
            }
        }
    }
    
    @objc private func hide() {
        UIView.animate(withDuration: 0.3) {
            self.extBgColor(color: .init(white: 0, alpha: 0.01))
            self.contentView.ks_y = self.ks_height
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
    
    @objc func sureBtnClick() {
        self.sureBlock?()
        
        hide()
    }
}
